package com.dailyblue.java.spring.ioc;

import com.dailyblue.java.spring.ioc.content.DemoA;
import com.dailyblue.java.spring.ioc.content.DemoB;
import com.dailyblue.java.spring.ioc.content.DemoC;
import com.dailyblue.java.spring.ioc.content.DemoD;
import com.dailyblue.java.spring.ioc.work.work1.ClassRoom;
import com.dailyblue.java.spring.ioc.work.work2.Printer;
import com.dailyblue.java.spring.ioc.work.work3.School;
import com.dailyblue.java.spring.ioc.work.work3.Student;
import com.dailyblue.java.spring.ioc.work.work3.Teacher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class App {

    private static void a() {
        // 加载xml文件 默认会产生配置文件中的所有对象
        ApplicationContext beans = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 获取到容器中的对象 按照名称 byName
        Object obj1 = beans.getBean("da");
        // 按照类型 获取对象 byType 缺陷：容器不能有多个DemoA类型的实例
        //DemoA obj2 = beans.getBean(DemoA.class);
        // 按照名称+类型的写法
        DemoA obj3 = beans.getBean("da2", DemoA.class);
        System.out.println(obj1);
        //System.out.println(obj2);
        System.out.println(obj3);
    }

    private static void b() {
        ApplicationContext beans = new FileSystemXmlApplicationContext("ioc-xml/src/main/resources/applicationContext.xml");
        DemoB db = beans.getBean("db", DemoB.class);
        System.out.println(db);
    }

    private static void c() {
        // 加载xml文件 默认会产生配置文件中的所有对象
        ApplicationContext beans = new ClassPathXmlApplicationContext("applicationContext.xml");
        DemoC dc = beans.getBean("dc", DemoC.class);
        System.out.println(dc);
    }

    private static void d() {
        ApplicationContext beans = new ClassPathXmlApplicationContext("applicationContext.xml");
        DemoD dd = beans.getBean(DemoD.class);
        System.out.println(dd);
    }

    private static void e(){
        ApplicationContext beans = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("---------------------->开始获取对象");
        Object obj1 = beans.getBean("de");
        Object obj2 = beans.getBean("de");
        System.out.println(obj1);
        System.out.println(obj2);
    }

    private static void f(){
        ApplicationContext beans = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("---------------------->开始获取对象");
        /*Object obj1 = beans.getBean("df");
        Object obj2 = beans.getBean("df");
        System.out.println(obj1);
        System.out.println(obj2);*/

    }
    // 作业
    private static void work1(){
        ApplicationContext beans = new ClassPathXmlApplicationContext("work.xml");
        ClassRoom cr = beans.getBean("classRoom", ClassRoom.class);
        System.out.println(cr);
    }

    private static void work2(){
        ApplicationContext beans = new ClassPathXmlApplicationContext("work.xml");
        Printer printer = beans.getBean(Printer.class);
        printer.print("shdfksdgflksdgfjklgsdlfkgdfgdsjfgdfgdjsgfjsdgfdsgfkjlsdgfksdfgdskjfgsdkfgdsjfg");
    }
    private static void work3(){
        ApplicationContext beans = new ClassPathXmlApplicationContext("work.xml");
        Teacher teacher = beans.getBean(Teacher.class);
        School school = beans.getBean(School.class);
        Student student = beans.getBean(Student.class);
        school.print(student);
    }
    public static void main(String[] args) {
        work3();
    }
}
